package com.jfeat.am.module.wegou.api.shop.crud;

import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.jfeat.am.common.constant.tips.ErrorTip;
import com.jfeat.am.common.constant.tips.SuccessTip;
import com.jfeat.am.common.constant.tips.Tip;
import com.jfeat.am.common.controller.BaseController;
import com.jfeat.am.core.support.BeanKit;
import com.jfeat.am.module.wegou.api.patch.ProductStatus;
import com.jfeat.am.module.wegou.api.patch.RestaurantStatus;
import com.jfeat.am.module.wegou.constant.Identify;
import com.jfeat.am.module.wegou.exception.WegouBizException;
import com.jfeat.am.module.wegou.services.crud.service.ProductService;
import com.jfeat.am.module.wegou.services.crud.service.ProductTypeService;
import com.jfeat.am.module.wegou.services.domain.service.QueryProductService;
import com.jfeat.am.module.wegou.services.persistence.model.Product;
import com.jfeat.am.module.wegou.services.persistence.model.ProductStore;
import com.jfeat.am.module.wegou.services.persistence.model.ProductType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 料理 api
 * </p>
 *
 * @author Code Generator
 * @since 2018-01-03
 */
@Api("料理（料理店铺使用）")
@RestController
@RequestMapping("/api/shop/product")
public class ProductEndpointForShop extends BaseController {

    @Resource
    ProductService productService;

    @Resource
    QueryProductService queryProductService;

    @Resource
    ProductTypeService productTypeService;

    @ApiOperation(value = "料理 新建料理", response = Product.class)
    @PostMapping
    @com.jfeat.am.module.wegou.annotation.UserIdentity("STORE")
    public Tip createProduct(@RequestBody Product entity) {
        ProductStore productRestaurant = getAttr(Identify.PRODUCT_STORE);
        entity.setStoreId(productRestaurant.getId());
        entity.setStatus(ProductStatus.RISE.toString());
        return SuccessTip.create(productService.createMaster(entity));
    }

    @ApiOperation(value = "料理 查询料理详情", response = Product.class)
    @GetMapping("/{id}")
    @com.jfeat.am.module.wegou.annotation.UserIdentity("STORE")
    public Tip getProduct(@PathVariable Long id) {
        ProductStore productRestaurant = getAttr(Identify.PRODUCT_STORE);
        Product product = productService.retrieveMaster(id);
        ProductType productType = productTypeService.getById(product.getProductTypeId());
        Map<String, Object> map = BeanKit.beanToMap(product);
        map.put("zhTypeName", productType.getZhTypeName());

        if (!product.getStoreId().equals(productRestaurant.getId())) {
            ErrorTip.create(WegouBizException.NOT_YOUR_PRODUCT.getCode(),
                    WegouBizException.NOT_YOUR_PRODUCT.getMessage());
        }
        return SuccessTip.create(map);
    }

    @ApiOperation(value = "料理 修改料理信息", response = Product.class)
    @PutMapping("/{id}")
    @com.jfeat.am.module.wegou.annotation.UserIdentity("STORE")
    public Tip updateProduct(@PathVariable Long id, @RequestBody Product entity) {
        ProductStore productRestaurant = getAttr(Identify.PRODUCT_STORE);
        Product product = productService.retrieveMaster(id);
        if (!product.getStoreId().equals(productRestaurant.getId())) {
            ErrorTip.create(WegouBizException.NOT_YOUR_PRODUCT.getCode(),
                    WegouBizException.NOT_YOUR_PRODUCT.getMessage());
        }
        entity.setId(id);
        return SuccessTip.create(productService.updateMaster(entity));
    }

    @ApiOperation(value = "料理 修改料理状态", response = Product.class)
    @PutMapping("/updateStatus/{id}")
    @com.jfeat.am.module.wegou.annotation.UserIdentity("STORE")
    public Tip updateProductStatus(@PathVariable Long id) {
        Product product = productService.retrieveMaster(id);
        if (product.getStatus().equals(RestaurantStatus.RISE.toString())) {
            product.setStatus(RestaurantStatus.DOWN.toString());
        } else {
            product.setStatus(RestaurantStatus.RISE.toString());
        }
        return SuccessTip.create(productService.updateMaster(product));
    }

    @ApiOperation(value = "料理 新建删除料理信息", response = Product.class)
    @DeleteMapping("/{id}")
    @com.jfeat.am.module.wegou.annotation.UserIdentity("STORE")
    public Tip deleteProduct(@PathVariable Long id) {
        ProductStore productRestaurant = getAttr(Identify.PRODUCT_STORE);
        Product product = productService.retrieveMaster(id);
        if (!product.getStoreId().equals(productRestaurant.getId())) {
            ErrorTip.create(WegouBizException.NOT_YOUR_PRODUCT.getCode(),
                    WegouBizException.NOT_YOUR_PRODUCT.getMessage());
        }
        return SuccessTip.create(productService.deleteMaster(id));
    }

    @ApiOperation(value = "料理 查询料理", response = Product.class)
    @GetMapping
    @com.jfeat.am.module.wegou.annotation.UserIdentity("STORE")
    public Tip queryProducts(Page<Map<String, Object>> page,
                             @RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                             @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                             @RequestParam(name = "status", required = false) String status,
                             @RequestParam(name = "typeName", required = false) String typeName,
                             @RequestParam(name = "zhTypeName", required = false) String zhTypeName,
                             @RequestParam(name = "name", required = false) String name,
                             @RequestParam(name = "zhName", required = false) String zhName,
                             @RequestParam(name = "productTypeId", required = false) Long productTypeId) {
        page.setSize(pageSize);
        page.setCurrent(pageNum);
        ProductStore productRestaurant = getAttr(Identify.PRODUCT_STORE);
        page.setRecords(queryProductService.findProductByType(page, status, typeName, zhTypeName, name, zhName, productTypeId, productRestaurant.getId()));
        return SuccessTip.create(page);
    }

    @ApiOperation(value = "打印菜单列表",response = Map.class)
    @GetMapping("/list")
    @com.jfeat.am.module.wegou.annotation.UserIdentity("STORE")
    public Tip getPrinterList(){
        ProductStore productRestaurant = getAttr(Identify.PRODUCT_STORE);
        String status = "RISE";
        List<Map<String,Object>> maps = queryProductService.findProductByType(status, null, null, null, null, null, productRestaurant.getId());
        List<String> typeNames = Lists.newArrayList();
        if (maps.size() > 0){
            maps.forEach(item -> {
                if (!typeNames.contains(item.get("typeName").toString())){
                    typeNames.add(item.get("typeName").toString());
                }
            });
        }
        List<List> lists = Lists.newArrayList();
        typeNames.forEach(s -> {
            List<Map<String,Object>> list = Lists.newArrayList();
            maps.forEach(item -> {
                if (item.get("typeName").toString().equals(s)){
                    list.add(item);
                }
            });
            lists.add(list);
        });
        return SuccessTip.create(lists);
    }
}
